package service

import (
	"github.com/asaskevich/govalidator"
	"github.com/gin-gonic/gin"
	"imProject/model"
	"imProject/utils"
	"math/rand"
	"strconv"
)

// 定义swagger前端页面的 各种按键

// @GetUserList
// @summary Show All Users
// @Tags 用户模块
// PingExample godoc
// @Success 200 {string} json{"code"; "message"}
// @Router /user/getUserList [get]
func GetUserList(c *gin.Context) {
	c.JSON(200, gin.H{
		"message": model.GetUserListInMySQL(),
	})
}

// @CheckLogin
// @summary CheckLogin
// @Tags 用户模块
// PingExample godoc
// @param Name formData string false "Name"
// @param Password formData string false "Password"
// @Success 200 {string} json{"code"; "LoginSuccess"}
// @Router /user/checkLogin [post]
func CheckLogin(c *gin.Context) {
	user := model.UserBasic{}
	name := c.PostForm("Name")
	password := c.PostForm("Password")

	user = model.FindUserInName(name)
	if user.Name == "" {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "User Not Registered",
			"data":    user.Name,
		})
		return
	}

	checkPwd := utils.MakePasswdDecrypt(password, user.Salt, user.Password)
	if !checkPwd {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Password Incorrect",
			"data":    password,
		})
		return
	}

	// 使用输入密码和存入的salt资源对密码加密 校验是否与数据库中录入的加密密码相同
	encryptPwd := utils.MakePasswdEncrypt(password, user.Salt)
	findUser := model.CheckLogin(name, encryptPwd)

	c.JSON(200, gin.H{
		"code":    0,
		"message": "Login Success!",
		"data":    findUser,
	})
}

// @CreateNewUser
// @summary createNewUser
// @Tags 用户模块
// @param Name query string false "Name"
// @param Password query string false "Password"
// @param RePassword query string false "RePassword"
// @param Email query string  false "Email"
// @param Phone query string false  "Phone"
// PingExample godoc
// @Success 200 {string} json{"code"; "message"}
// @Router /user/createNewUser [get]
func CreateUser(c *gin.Context) {
	user := model.UserBasic{}
	user.Name = c.Query("Name")
	user.Email = c.Query("Email")
	user.Phone = c.Query("Phone")
	password := c.Query("Password")
	rePassword := c.Query("RePassword")

	// 检查用户名，用户电话，邮箱是否被注册
	checkName := model.FindUserInName(user.Name)
	checkPhone := model.FindUserInPhone(user.Phone)
	checkEmail := model.FindUserInEmail(user.Email)

	// 定义随机数，添加到用户的 salt字段， 作为密码加密的随机数
	randNum := rand.Int31()
	randNumStr := strconv.Itoa(int(randNum))
	user.Salt = randNumStr

	if checkName.Name != "" {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Name already exists",
			"data":    user.Name,
		})
		return
	}

	if checkPhone.Phone != "" {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Phone already exists",
			"data":    user.Phone,
		})
		return
	}

	if checkEmail.Email != "" {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Email already exists",
			"data":    user.Email,
		})
		return
	}
	if password != rePassword {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Password Confirm Failed!",
		})
		return
	} else {
		user.Password = utils.MakePasswdEncrypt(password, randNumStr)

	}

	model.CreateUserBasic(&user)
	c.JSON(200, gin.H{
		"code":    0,
		"message": "Create New User Success!",
	})
}

// @deleteUser
// @summary deleteUser
// @Tags 用户模块
// @param ID query string false "ID"
// @param name query string false "name"
// PingExample godoc
// @Success 200 {string} json{"code"; "message"}
// @Router /user/deleteUser [get]
func DeleteUser(c *gin.Context) {
	user := model.UserBasic{}
	userID, err := strconv.Atoi(c.Query("ID"))
	uintUserId := uint(userID)
	if err != nil {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Delete User Failed!",
			"data":    err.Error(),
		})
	} else {
		user.ID = uintUserId
	}

	user.Name = c.Query("name")

	model.DeleteUserBasic(&user)
	c.JSON(200, gin.H{
		"code":    0,
		"message": "Delete  User Success!",
	})
}

// @UpdateUser
// @summary UpdateUser
// @Tags 用户模块
// @param ID formData string false "ID"
// @param name formData string false "name"
// @param Password formData string false "Password"
// @param phone formData string false "phone"
// @param email formData string false "email"
// PingExample godoc
// @Success 200 {string} json{"code"; "message"}
// @Router /user/updateUser [post]
func UpdateUser(c *gin.Context) {
	user := model.UserBasic{}
	userID, err := strconv.Atoi(c.PostForm("ID"))
	uintUserId := uint(userID)
	if err != nil {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Update User Failed!",
			"data":    err.Error(),
		})
	} else {
		user.ID = uintUserId
	}

	user.Name = c.PostForm("name")

	user.Password = c.PostForm("Password")

	user.Email = c.PostForm("email")

	user.Phone = c.PostForm("phone")

	_, err = govalidator.ValidateStruct(user)
	if err != nil {
		c.JSON(-1, gin.H{
			"code":    -1,
			"message": "Incorrect input format",
			"data":    err.Error(),
		})
	} else {

		model.UpdateUserBasic(&user)

		c.JSON(200, gin.H{
			"code":    0,
			"message": "Update  User Success!",
		})
	}

}
